import 'dart:convert';

import 'package:app/common/colors.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/services/address.dart';
import 'package:app/utils/hexcolor.dart';
import 'package:app/utils/persisten_storage.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../common/tool.dart';
import '../login_modules/seturl_page.dart';

class ServeSettingPage extends StatefulWidget {
  @override
  _ServeSettingPageState createState() => _ServeSettingPageState();
}

class _ServeSettingPageState extends State<ServeSettingPage> {
//授权页登录业务逻辑添加

  Map addressmap = {};



  @override
  void initState() {
    super.initState();
    print("ServeSettingPage==initState>>");
    getInitData();
  }

  void getInitData() async {
    print("getSInitData==print>>");
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var urlJson = prefs.getString("urlJson")!;
    addressmap = json.decode(urlJson);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:BaseAppBar(
        child: Text('服务器'),
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        action: [
              TextButton(
                  onPressed: () async {
                    var data = await Get.to(SetUrlPage(position: -1));
                    if(data == 'refresh'){
                      getInitData();
                    }
                  },
                  child: Icon(Icons.add,color: Colors.white,size: 25))
        ],
      ),
      // AppBar(
      //   leading: IconButton(
      //     onPressed: () {
      //       Get.back();
      //     },
      //     icon: Image.asset(
      //       'images/leftarr.png',
      //       color: Colors.white,
      //       width: 15,
      //       height: 15,
      //     ),
      //   ),
      //   title: const Text('服务器'),
      //   actions: [
      //     TextButton(
      //         onPressed: () async {
      //           var data = await Get.to(SetUrlPage(position: -1));
      //           if(data == 'refresh'){
      //             getInitData();
      //           }
      //         },
      //         child: Icon(Icons.add,color: Colors.white,size: 32))
      //   ],
      //   backgroundColor: AppColor.themeColor,
      // ),
      body: ListView.separated(
          itemBuilder: (context, index) {
            return GestureDetector(
              onTap: () async{
                print("index==>>>$index==>>");
               var data = await Get.to(SetUrlPage(position: index));
               if(data == 'refresh'){
                 getInitData();
               }
              },
              child: Container(
                margin: const EdgeInsets.all(
                  12,
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(children: [
                      Container(
                        margin: const EdgeInsets.all(
                          8,
                        ),
                        child: Image.asset(
                          'images/dtcloud_icon_serve.png',
                          width: 48,
                          height: 48,
                        ),
                      ),
                      Container(
                        height: 56,
                        margin: const EdgeInsets.only(
                          left: 10,
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children:[
                            const Text("服务器名称"),
                            Text(addressmap[index.toString()],style: TextStyle(color: Colors.grey,fontSize: 14)),
                          ],
                        ),
                      ),
                    ]),
                    Container(
                      margin: const EdgeInsets.all(
                        12,
                      ),
                      child: Image.asset(
                        'images/dtcloud_edit_serve.png',
                        width: 24,
                        height: 24,
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
          separatorBuilder: (context, index) {
            return const Divider(indent: 20, endIndent: 20);
          }, //分隔线
          itemCount: addressmap.length),
    );
  }
}
